package com.pet_service.service.impl;

import com.pet_service.entity.*;
import com.pet_service.enums.*;
import com.pet_service.mapper.FosterOrderMapper;
import com.pet_service.mapper.HomeServiceTypeMapper;
import com.pet_service.mapper.OrderHomeServiceMapper;
import com.pet_service.mapper.OrdersMapper;
import com.pet_service.result.Result;
import com.pet_service.service.BookingService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.UUID;

/**
 * @author 邹全伸
 * @version 1.0
 * @date 2025-10-06 10:36
 */
@Service
public class BookingServiceImpl implements BookingService {

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private OrderHomeServiceMapper orderHomeServiceMapper;

    @Resource
    private FosterOrderMapper fosterOrderMapper;

    @Resource
    private HomeServiceTypeMapper homeServiceTypeMapper;

    @Override
    public Result<String> createBooking(Booking booking) {
        try {
            // 1. 创建主订单
            Orders order = new Orders();
            order.setOrder_no("ORDER_" + UUID.randomUUID().toString().substring(0, 8));
            order.setUser_id(booking.getUserId());
            order.setPet_id(booking.getPetId());
            order.setTotal_amount(new BigDecimal("0")); // 暂存，后续计算
            order.setPay_amount(new BigDecimal("0"));
            order.setOrder_status(TOrderStatus.PENDING); // 代付款
            order.setService_type(booking.getServiceTypeId() == 1 ? TOrderServiceType.HOME_SERVICE : TOrderServiceType.FOSTER_SERVICE);
            order.setPay_status(TPayStatus.UNPAID); // 未支付
            order.setPay_type(null); // 未选择支付方式
            order.setPay_time(null); // 未支付，无时间
            order.setTransaction_no(null); // 无交易流水号

            ordersMapper.insert(order);

            Integer orderId = order.getId();

            if (booking.getServiceTypeId() == 1) { // 上门服务
                // 查询服务价格
                HomeServiceType serviceType = homeServiceTypeMapper.selectById(booking.getServiceTypeId());
                order.setTotal_amount(serviceType.getService_price());

                // 插入上门服务明细
                OrderHomeService homeService = new OrderHomeService();
                homeService.setOrder_id(orderId);
                homeService.setService_type_id(booking.getServiceTypeId());
                homeService.setService_time(java.sql.Timestamp.valueOf(booking.getServiceTime()));
                homeService.setService_address_id(booking.getAddressId());
                homeService.setService_status(TOrderHomeServiceStatus.PENDING);
                orderHomeServiceMapper.insert(homeService);
            } else if (booking.getServiceTypeId() == 2) { // 寄养服务
                FosterOrder fosterOrder = new FosterOrder();
                fosterOrder.setOrder_id(orderId);
                fosterOrder.setHospital_id(booking.getHospitalId());
                fosterOrder.setPet_info("宠物信息待补充");
                fosterOrder.setFoster_start_time(java.sql.Timestamp.valueOf(booking.getServiceTime()));
                fosterOrder.setStatus(TFosterOrderStatus.PENDING);
                fosterOrderMapper.insert(fosterOrder);
            }

            return Result.ok("预约成功");
        } catch (Exception e) {
            return Result.fail("预约失败：" + e.getMessage());
        }
    }
}

